package com.zyw.service.procurement.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.ProcurementMaterialAnalysis;
import com.zyw.from.ProcurementFrom;
import com.zyw.mapper.procurement.ProcurementMaterialAnalysisMapper;
import com.zyw.service.procurement.ProcurementMaterialAnalysisService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ProcurementMaterialAnalysisServiceImpl implements ProcurementMaterialAnalysisService {
    @Resource
    public ProcurementMaterialAnalysisMapper procurementMaterialAnalysisMapper;


    @Override
    public LayUITableVO<List<ProcurementMaterialAnalysis>> findPage(Map<String, Object> map) {

        ProcurementFrom from = (ProcurementFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));


        List<ProcurementMaterialAnalysis> list = procurementMaterialAnalysisMapper.findPage(map);

        PageInfo<ProcurementMaterialAnalysis> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ProcurementMaterialAnalysis>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public void delectBy(String id, String liningId) {
        Example example = new Example(ProcurementMaterialAnalysis.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("orderId",id);
        c.andEqualTo("liningId",liningId);
        procurementMaterialAnalysisMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public void save(List<ProcurementMaterialAnalysis> dataList) {
        for (ProcurementMaterialAnalysis data : dataList) {
            Example example = new Example(ProcurementMaterialAnalysis.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("orderId",data.getOrderId());
            c.andEqualTo("liningId",data.getLiningId());
            List<ProcurementMaterialAnalysis> procurementMaterialAnalyses = procurementMaterialAnalysisMapper.selectByExample(example);
            if (procurementMaterialAnalyses.size()>0){
                procurementMaterialAnalysisMapper.updateByExample(data,example);
            }else {
                procurementMaterialAnalysisMapper.insertSelective(data);
            }
        }
    }

    @Override
    public List<ProcurementMaterialAnalysis> selectByOrderId(String orderId) {
        Example example = new Example(ProcurementMaterialAnalysis.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("orderId",orderId);
        List<ProcurementMaterialAnalysis> procurementMaterialAnalyses = procurementMaterialAnalysisMapper.selectByExample(example);
        return procurementMaterialAnalyses;
    }
}
